package com.cqust.wdm.emsboot.service.impl;

import com.cqust.wdm.emsboot.domain.*;
import com.cqust.wdm.emsboot.mapper.OrderMapper;
import com.cqust.wdm.emsboot.mapper.ProductMapper;
import com.cqust.wdm.emsboot.service.IOrderService;
import com.cqust.wdm.emsboot.util.BizException;
import com.cqust.wdm.emsboot.util.RandomValidateCodeUtil;
import com.cqust.wdm.emsboot.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 订单信息Service业务层处理
 *
 * @author zc
 */
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductServiceImpl productService;


    /**
     * 查询订单信息
     *
     * @param id 订单信息ID
     * @return 订单信息
     */
    @Override
    public Order selectOrderById(Long id) {
        return orderMapper.selectOrderById(id);
    }

    @Override
    public List<CustomerTO> selectSumCountGroupByUid(Order order) {
        return orderMapper.selectSumCountGroupByUid(order);
    }

    /**
     * 查询订单信息列表
     *
     * @param order 订单信息
     * @return 订单信息
     */
    @Override
    public List<Order> selectOrderList(Order order) {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增订单信息
     *
     * @param order 订单信息
     * @return 结果
     */
    @Override
    public int insertOrder(Order order) {
        if (order.getState() == 1) {
            Product product = productMapper.selectProductById(Long.valueOf(order.getPid()));
            if (product.getBalance() < order.getCount()) {

                throw new BizException("505", "库存不足");

            }
        }
        order.setCreateBy(UserUtil.getCurrentUser().getUsername());
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单信息
     *
     * @param order 订单信息
     * @return 结果
     */
    @Override
    public int updateOrder(Order order) {
        //出库--兼容安卓
        if (order.getAuditFlag() == 3) {
            System.out.println("扫码通过、操作库存");
            order = selectOrderById(order.getId());
            Product product = new Product();
            product.setId(Long.valueOf(order.getPid()));
            product.setBalance(order.getCount());
            order.setAuditFlag(3);
            productService.reduceProductBalance(product);
        } else if (order.getState() == 1) {
            //出库
            if (order.getAuditFlag() == 3) {
                System.out.println("扫码通过、操作库存");
                order = selectOrderById(order.getId());
                Product product = new Product();
                product.setId(Long.valueOf(order.getPid()));
                product.setBalance(order.getCount());
                order.setAuditFlag(3);
                productService.reduceProductBalance(product);
            } else if (order.getAuditFlag() == 1) {//审核通过
                order.setExtractionCode(RandomValidateCodeUtil.generateVerifyCode(8));
                System.out.println("取货码:" + order.getExtractionCode());

            }
        } else {  //入库
            if (order.getAuditFlag() == 1) {//审核通过
                order = selectOrderById(order.getId());
                Product product = new Product();
                product.setId(Long.valueOf(order.getPid()));
                product.setBalance(order.getCount());
                order.setAuditFlag(1);
                productService.addProductBalance(product);
            }
        }
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单信息
     *
     * @param ids 需要删除的订单信息ID
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Long[] ids) {
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单信息信息
     *
     * @param id 订单信息ID
     * @return 结果
     */
    @Override
    public int deleteOrderById(Long id) {
        return orderMapper.deleteOrderById(id);
    }

    @Override
    public List<OrderOut> selectOrderOutList(Order order) {
        return orderMapper.selectOrderOutList(order);
    }

    @Override
    public String importOrder(List<OrderExcel> excelList, boolean updateSupport) {
        for (int i = 0; i < excelList.size(); i++) {
            OrderExcel orderExcel = excelList.get(i);
            Order order = new Order();
            order.setRemark(orderExcel.getRemark());
            order.setCount(orderExcel.getCount());
            order.setPid(orderExcel.getPid());
            order.setState(orderExcel.getState());
            insertOrder(order);

        }
        System.out.println(excelList.size());
        return "null";
    }
}
